/**
 * LoanProcessing.java
 * 
 * author: Zhiqiang Zhu
 * date: Nov/27/2020
 */

package controllers;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import models.DaoModel;
import models.Serializer;
import java.util.Date;
import records.BankRecords;
import views.LoanConsole;
import views.LoanView;

/**
 * class LoanProcessing The class LoanController contains the main and therefore
 * is the driver file of the program.
 */
public class LoanProcessing extends BankRecords {

	/**
	 * @param args @throws
	 */
	public static void main(String[] args) {
		String serFileName = "bankrecords.ser";
		BankRecords br = new BankRecords();
		br.readData();

		DaoModel dao = new DaoModel();
		dao.createTable();
		BankRecords robjs[] = BankRecords.CopyOfRecords();
		dao.insertRecords(robjs); // perform inserts

		// views
		try {
			ResultSet rs;
			rs = dao.retrieveRecords();
			new LoanView().runView(rs);
			rs.close();
		} catch (SQLException se) {
			se.printStackTrace();
		}

		// Collect BankRecords into a HashMap
		Map<String,BankRecords> bankRecordsToSave;
		bankRecordsToSave = new HashMap<String,BankRecords>();
		for(BankRecords b: robjs){
			bankRecordsToSave.put(b.getId(), b);
		}
		// current timestamp
		PersistentObject po = new PersistentObject(new Date(), bankRecordsToSave);
		// Serialize PersistentObject to a file
		FileOutputStream outStream = null;
		ObjectOutputStream objectOutputFile = null;
		try {
			outStream = new FileOutputStream(serFileName);
			objectOutputFile = new ObjectOutputStream(outStream);
			objectOutputFile.writeObject(po);
		} catch (FileNotFoundException e2) {
			System.out.println(e2.getMessage());
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
		}

		// Make application sleep for 5 seconds
		try {
			System.out.println("Sleeping...");
			Thread.sleep(5000);
			System.out.println("5 seconds sleep completed.");
		} catch (InterruptedException e) {
			System.out.println(e.getMessage());
		}
		// Deserialize the persistent object
		FileInputStream inStream = null;
		ObjectInputStream objectInputStream = null;
		try {
			inStream = new FileInputStream(serFileName);
			objectInputStream = new ObjectInputStream(inStream);
		} catch (FileNotFoundException e1) {
			System.out.println(e1.getMessage());
		} catch (IOException e2) {
			System.out.println(e2.getMessage());
		}

		// new PersistentObject object, to store deserialized information
		PersistentObject new_po = null;
		try {
			new_po = (PersistentObject) objectInputStream.readObject();
		} catch (ClassNotFoundException | IOException e) {
			System.out.println(e.getMessage());
		}
		
		// new date object obtained from deserialization of file
		Date doneTime = new_po.getDate();

		// Display to the console the time difference between serialization and deserialization
		System.out.println("\nTime difference between serialization and deserialization: "
				+ (((new Date()).getTime() - doneTime.getTime()) / 1000.00) + " seconds");
		
		// console view output
		System.out.println("Console View:");
		try {
			ResultSet rs;
			rs = dao.retrieveRecords();
			new LoanConsole().output(rs);
			rs.close();
		} catch (SQLException se) {
			se.printStackTrace();
		}

	}

}
